package com.example.admin_oa.model.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.admin_oa.model.mapper.SysPermissionMapper;
import com.example.admin_oa.model.mapper.SysUserRoleMapper;
import com.example.admin_oa.model.service.SysUserService;
import com.example.admin_oa.model.mapper.SysUserMapper;
import com.example.admin_oa.shiro.JwtToken;
import com.example.admin_oa.shiro.JwtUtil;
import com.example.comment_oa.pojos.SysRole;
import com.example.comment_oa.pojos.SysUser;
import com.example.comment_oa.pojos.SysUserRole;
import com.example.comment_oa.util.EaxyUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.checkerframework.checker.units.qual.A;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
* @author cjl
* @description 针对表【sys_user】的数据库操作Service实现
* @createDate 2022-10-18 16:26:34
*/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService{
    @Resource
    SysPermissionMapper sysPermissionMapper;
    @Resource
    StringRedisTemplate stringRedisTemplate;
    @Resource
    SysUserMapper sysUserMapper;
    @Resource
    SysUserRoleMapper sysUserRoleMapper;

    @Override
    public SysUser newOne() {
        QueryWrapper<SysUser> wrapper=new QueryWrapper<>();
        wrapper.orderByDesc("user_id");
        wrapper.last("limit 1");
        SysUser sysUser = sysUserMapper.selectOne(wrapper);
        String substring = sysUser.getUserNo().substring(4);
        int i = Integer.parseInt(substring);
        return sysUser;
    }

    @Override
    public SysUser login(String username, String userPassword) {
        String jwt = JwtUtil.createJWT(username, "back", "user", 1000 * 60 * 60 *2);
        QueryWrapper<SysUser> wrapper=new QueryWrapper<>();
        wrapper.eq("user_no",username).or().eq("user_phone",username);
        SysUser sysUser = sysUserMapper.selectOne(wrapper);
        stringRedisTemplate.opsForValue().set("token",jwt,2, TimeUnit.HOURS);
        return sysUser;
    }

    @Override
    public List<SysRole> selectByUserNo(String userNo) {
        List<SysUser> sysUsers = sysUserMapper.selectByUserNo(userNo);
        List<SysRole> list=new ArrayList<>();
        sysUsers.forEach(r->{
            r.getSysUserRole().forEach(e->{
                list.add(e.getSysRole());
            });
        });
        return list;
    }

    @Override
    public int updateUserRole(String[] list) {
        QueryWrapper<SysUserRole> wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",list[0]);
        sysUserRoleMapper.delete(wrapper);
        List<SysUserRole> sysUserRoleList=new ArrayList<>();
        if(list.length>=2){
            for (int i = 1; i < list.length; i++) {
                SysUserRole sysUserRole=new SysUserRole(Integer.parseInt(list[0]),Integer.parseInt(list[i]));
                sysUserRoleList.add(sysUserRole);
            }
            return sysUserRoleMapper.insertBatch(sysUserRoleList);
        }
        return 0;
    }
}




